// pages/changeMyInfo/changeMyInfo.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {   
      username:"11111",
      name:"11111", 
      age: 11111,  
      sex: "男",  
      phone: 11111,  
      address: "11111",
  },  


  bindSexChange: function(e){
    console.log(e.detail.value)
    this.setData({
      sex:e.detail.value == 0 ? "男":"女",
    })
  },

  handleSubmit: function (e) {  
    // 获取表单提交的数据  
    const formData = e.detail.value;  
    // 在这里，你可以将formData发送到后端进行更新操作  
    console.log(formData);  
    // 如果后端更新成功，你可能需要给用户一个反馈  
    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo)
    wx.request({
      url: 'http://localhost:9090/api/userInfo/update',
      "Content-Type": "application/x-www-form-urlencoded",
      method:"POST",
      data:{
        id:userInfo.id,
        username:userInfo.username,
        name:formData.name,
        age:formData.age,
        sex:formData.sex == 0?'男':'女',
        phone:formData.phone,
          address:formData.address,
      },
      success:(res)=>{
        console.log(res.data)
        wx.setStorageSync("userInfo",res.data.data)
        let newUserInfo = wx.getStorageSync('userInfo')
        this.setData({
          username:newUserInfo.username,
          name:newUserInfo.name,
          age:newUserInfo.age,
          sex:newUserInfo.sex,
          phone:newUserInfo.phone,
          address:newUserInfo.address,
        })
        wx.showToast({  
          title: '更新成功',  
          icon: 'success',  
          duration: 2000  
        }); 
      },
      fail(res){
        wx.showToast({
          title: '更新失败！',
          icon:"none",
          duration:2000
        })          
      }
    })
       
  }  ,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      username:userInfo.username,
      name:userInfo.name,
      age:userInfo.age,
      sex:userInfo.sex,
      phone:userInfo.phone,
      address:userInfo.address,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})